package com.examsystem.controller;

import com.alibaba.fastjson.JSON;
import com.examsystem.pojo.Paper;
import com.examsystem.pojo.vo.LoginUser;
import com.examsystem.pojo.vo.PaperVo;
import com.examsystem.pojo.vo.Result;
import com.examsystem.service.PaperService;
import com.examsystem.util.JwtUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author fire'ge
 */
@Controller
@RequestMapping("/paper")
public class PaperController {
    @Resource
    private PaperService paperService;

    @PreAuthorize("hasAuthority('paper:allPaper')")
    @RequestMapping(value = "/allPaper")
    @ResponseBody
    public String list(HttpServletRequest request) {
        String token = request.getHeader("token");
        int userId = JwtUtil.getJWTUserId(token);
        List<Paper> papers = paperService.queryPaperByUserId(userId);
            System.out.println(papers);
        Result result = Result.succ(papers);
        return JSON.toJSONString(result);
    }


        /*
        教师端代码
         */


    /**
     * 分页显示所有的试卷【每页10条】
     * @param pageNum  页码
     * @return result结果集
     */
    @PreAuthorize("hasAuthority('paper:selectPaperName')")
    @RequestMapping("/selectPaperName")
    @ResponseBody
    public Result selectPaperName(int pageNum,int status) {
        PageInfo<Paper> paperPageInfo = paperService.showAllPaper(pageNum);
        return Result.succ(paperPageInfo);
    }


    /**
     * 查询整张试卷
     * @param paperId  试卷id
     * @return result结果集
     */
    @PreAuthorize("hasAuthority('paper:paperDetails')")
    @RequestMapping("/paperDetails/{paperId}")
    @ResponseBody
    public Result paperDetail(@PathVariable int paperId) {
        PaperVo paperVo = paperService.showOnePaper(paperId);
        return Result.succ(paperVo);
    }


    /**
     * 新建试卷
     * @param paperName  试卷名
     * @param teacherId  教师id
     * @param questionId  试题id集合
     * @param choice  【1：手动 2：自动】
     * @param number  试题的数量
     * @return result结果集
     */
//    @PreAuthorize("hasAuthority('paper:newPaper')")
    @RequestMapping("/newPaper/{teacherId}")
    @ResponseBody
    public Result newPaper(String paperName, @PathVariable int teacherId,@RequestParam("questionId") List<Integer> questionId, int choice, int number) {
        if(questionId == null){
            return Result.fail("questionId为空~~");
        }
        int res = paperService.newPaper(paperName, teacherId, questionId, choice, number);
        if (res >= 1) {
            return Result.succ(true);
        }
        return Result.fail("新建试卷失败！");
    }


    /**
     * 显示一张试卷的试题
     *
     * @param paperId 试卷id
     * @return result对象
     */
    @RequestMapping("/showOnePaper/{paperId}")
    @ResponseBody
    public Result showOnePaper(@PathVariable int paperId) {
        PaperVo paper = paperService.showOnePaper(paperId);
        return Result.succ(paper);
    }


    /**
     * 交卷
     * @param paperId 试卷id
     * @param studentId  学生id
     * @param classId  班级id
     * @return  result对象
     */
    @PreAuthorize("hasAuthority('paper:putInPaper')")
    @RequestMapping("/putInPaper/{paperId}/{studentId}/{classId}")
    @ResponseBody
    public Result putInPaper(@PathVariable int paperId, @PathVariable int studentId, @PathVariable int classId) {
        return Result.succ(paperService.putInPaper(paperId, studentId, classId));
    }
}

